<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 申领单页面
 * @package  htdocs
 * @subpackage  company
 * @author  create by allen.qiang
 */

quanli_require('module/base/company/cardApply.class.php');
quanli_require('module/base/company/cardFaceValue.class.php');
quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/system/sysDepartment.class.php');
quanli_require('module/base/system/sysUser.class.php');
quanli_require('module/base/system/itemHandler.class.php');

class pa_cardApply {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: // 申领单列表
                    $this->cardApplyList();
                    break;

                case 2://编辑申领单
                    $this->saveCardApply();
                    break;

                case 3://申领单发卡
                    $this->tuckCard();
                    break;

                case 4://申领单开票
                    $this->outInvoiceCard();
                    break;

                case 5://申领单回款
                    $this->ReturnedMoney();
                    break;

                default:
                    backendError();
                    break;
            }

        } else {

            backendError();

        }
    }

    /**
     *申领单列表页
     */
    private function cardApplyList () {

        global $G_PGLOB;

        //实例用户类
        $sysUser = new sysUser( $G_PGLOB->DBCA );
        //当前登录用户信息
        $args_array = array(
            'userID' => $G_PGLOB->currentUserID
        );
        $userInfo_array = $sysUser->searchUserInfo( $args_array );
        $departmentID = $userInfo_array[0]['departmentID'];
        $isSalesman = $userInfo_array[0]['isSalesman'];
        if ( empty( $departmentID ) || !is_numeric( $departmentID )) {
            backendError('您不属于任何部门，无权查看');
        }
        if ( empty( $isSalesman ) || !is_numeric( $isSalesman )) {
            backendError('您不是申领人，无权查看');
        }

        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();


        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1, $departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);


        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(70004));
        $G_PGLOB->smarty->display('company/cardApplyList.tpl');

    }

    /*
 *
 * 申领单
 */
    private function saveCardApply(){

        global $G_PGLOB;

        //实例申领单类
        $cardApply = new cardApply();

        //实例化现金卡面值类
        $cardFaceValue = new cardFaceValue();

        //实例基础设置类
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );

        //修改申领单信息
        if ( isset($_REQUEST['apply_id']) && quanli_is_int($_REQUEST['apply_id']) ) {

            //查找申领单信息
            $arg_array = array(
                'apply_id' => $_REQUEST['apply_id']
            );
            $card_apply_array = $cardApply->search( $arg_array ) ;
            if (!$card_apply_array) {
                backendError('申领单信息没有找到', 404);
            }

            //查询申领单现金卡信息
            $card_detail_array = $cardApply->searchCardApplyDetail($arg_array);

            $back_arr=explode( ',', $card_apply_array[0]['paymentID']);
            $G_PGLOB->smarty->assign_by_ref('backArr', $back_arr);


            $G_PGLOB->smarty->assign_by_ref('card_apply_array', $card_apply_array[0]);
            $G_PGLOB->smarty->assign_by_ref('card_detail_array', $card_detail_array);

        }

        //查询面值信息
        $arg_array = array();
        $fave_value_array = $cardFaceValue->search( $arg_array );
        $G_PGLOB->smarty->assign_by_ref('fave_value_array', $fave_value_array);

        $args_array = array(
            'itemCategoryID' => 26,
            'useFlg' => 1
        );
        //获取回款方式
        $backSql='select itemID,itemName from  sys_item  WHERE itemCategoryID=27';
        $backArray=$G_PGLOB->DBCA->stmt_query($backSql);
        $G_PGLOB->smarty->assign('backArray', $backArray );

        //获取现金卡规格
        $card_spec_array = $itemHandler->searchItem( $args_array );
        $G_PGLOB->smarty->assign('card_spec_array', $card_spec_array );


        // add by sunqiang 20150416
        quanli_require('module/base/company/cardType.class.php');
        $cardType = new cardType();
        $cardType_arg = array();
        $cardType_array = $cardType->search( $cardType_arg );
        $G_PGLOB->smarty->assign_by_ref('cardType_array', $cardType_array);



        $G_PGLOB->smarty->display('company/cardApplyAdd.tpl');

    }

    /**
     *现金卡发卡页
     */
    private function tuckCard () {

        if ( !isset($_REQUEST['apply_id']) || !is_numeric($_REQUEST['apply_id']) ) {
            backendError('参数错误');
        }

        global $G_PGLOB;

        //实例化申领单类
        $cardApply = new cardApply() ;

        //查询申领单信息
        $arg_array = array(
            'apply_id' => $_REQUEST['apply_id']
        );
        $card_apply_array = $cardApply->search($arg_array);
        if (!$card_apply_array) {
            backendError('申领单信息没有找到', 404);
        }

        //查询申领单现金卡详情
        $card_detail_array = $cardApply->searchCardApplyDetail($arg_array);

        $G_PGLOB->smarty->assign_by_ref('card_apply_array', $card_apply_array[0]);
        $G_PGLOB->smarty->assign_by_ref('card_detail_array', $card_detail_array);

        $G_PGLOB->smarty->display('company/tuckCardList.tpl');


    }

    /**
     *现金卡开票页面
     */
    private function outInvoiceCard () {

        if ( !isset($_REQUEST['apply_id']) || !is_numeric($_REQUEST['apply_id']) ) {
            backendError('参数错误');
        }

        global $G_PGLOB;

        //实例化申领单类
        $cardApply = new cardApply() ;

        //查询申领单信息
        $arg_array = array(
            'apply_id' => $_REQUEST['apply_id']
        );
        $card_apply_array = $cardApply->search($arg_array);
        if (!$card_apply_array) {
            backendError('申领单信息没有找到', 404);
        }

        //查询申领单现金卡详情
        $card_detail_array = $cardApply->searchCardApplyDetail($arg_array);


        $G_PGLOB->smarty->assign_by_ref('card_apply_array', $card_apply_array[0]);
        $G_PGLOB->smarty->assign_by_ref('card_detail_array', $card_detail_array);

        //查询申领单开票信息
        $apply_invoice_array = $cardApply->searchCardApplyInvoice($arg_array);
        $G_PGLOB->smarty->assign_by_ref('apply_invoice_array', $apply_invoice_array);

        $G_PGLOB->smarty->assign('outInvoice_permission', $G_PGLOB->has_permission(70008));
        $G_PGLOB->smarty->display('company/outInvoiceCardList.tpl');

    }

    /**
     *申领但回款页面
     */
    private function ReturnedMoney () {

        if ( !isset($_REQUEST['apply_id']) || !is_numeric($_REQUEST['apply_id']) ) {
            backendError('参数错误');
        }

        global $G_PGLOB;

        //实例化申领单类
        $cardApply = new cardApply() ;

        //查询申领单信息
        $arg_array = array(
            'apply_id' => $_REQUEST['apply_id']
        );
        $card_apply_array = $cardApply->search($arg_array);
        if (!$card_apply_array) {
            backendError('申领单信息没有找到', 404);
        }

        //查询申领单现金卡详情
        $card_detail_array = $cardApply->searchCardApplyDetail($arg_array);


        $G_PGLOB->smarty->assign_by_ref('card_apply_array', $card_apply_array[0]);
        $G_PGLOB->smarty->assign_by_ref('card_detail_array', $card_detail_array);

        //实例基础设置类
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );

        //获取企业人数
        $args_array = array(
            'itemCategoryID' => 27,
            'useFlg' => 1
        );
        $returned_array = $itemHandler->searchItem( $args_array );
        $G_PGLOB->smarty->assign_by_ref('returned_array', $returned_array);

        //查询申领单回款信息
        $returned_money = $cardApply->searchCardApplyReturnedMoney($arg_array);
        $G_PGLOB->smarty->assign_by_ref('returned_money', $returned_money);
        $G_PGLOB->smarty->assign('returned_permission', $G_PGLOB->has_permission(70012));

        $G_PGLOB->smarty->display('company/ReturnedMoneyList.tpl');

    }

}